﻿using System.Collections.Generic;
using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.AchievementItem;
using Minecraft.Model.Backpack;
using Minecraft.Model.Dropinfo;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;

namespace Minecraft.ServerHall.Cmd
{
	public class AchievementsReceiveAward : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Achievements_AchievementsReceiveAward;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<AchievementsReceiveAwardReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：未登录）" });
				return;
			}
			if (req.achievementId <= 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：传参错误）" });
				return;
			}

			//1.检测此用户是否存在且已登录[已处理]
			//2.检测此成就是否存在
			var achievement = CsvConfig.achievementList.Find(t => t.AchievementId == req.achievementId); //此成就
			if (achievement == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：此成就不存在或玩家未参与）" });
				return;
			}
			//3.检测此用户是否已参与且已完成此成就
			var curUserAchievementItemsList = ModAchievementItemInfo.ConvertStr2List(session.minecraftSessionInfo.playerAchievement.AchievementDetailStr);


			var curAchievement = curUserAchievementItemsList.Find(t => t.AchievementId == req.achievementId);
			if (curAchievement == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：此成就状态有误）" });
				return;
			}

			if (curAchievement.Status != EnumAchievementStatus.Finished)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：此成就未完成）" });
				return;
			}

			//开始领奖了 20180521eve
			//todo
			//从这里开始调用yzz给我提供的把东西装背包里的函数
			//A B 
			//调用完成后，改变此用户的此成就的奖品的领取状态，内存中。
			List<DropinfoDropResourceInfo> dropinfoDropResourceInfoList = new List<DropinfoDropResourceInfo>();
			List<BackpackCellInfo> posGoodsinfoList = new List<BackpackCellInfo>();
			List<KeyValueEnumAttributeInt> dropAttributeList = new List<KeyValueEnumAttributeInt>();
			List<KeyValueEnumAttributeInt> attributeList = new List<KeyValueEnumAttributeInt>();


			var resp = session.InsertGoodsToBackpackFromDropId(achievement.Award, out dropinfoDropResourceInfoList, out posGoodsinfoList, out dropAttributeList, out attributeList);


			//var resp = new AchievementsReceiveAwardResp
			//{

			//};
			session.Send(defCommand, req, resp);
		}
	}
}
